package com.zhoudb.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhoudb.blog.dto.Nav;
import com.zhoudb.blog.entity.ArticleContent;
import com.zhoudb.blog.entity.Menu;
import com.zhoudb.blog.service.ArticleContentService;
import com.zhoudb.blog.service.MenuService;
import com.zhoudb.blog.util.ConversionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @Description:后台页面跳转
 * @Author: zhoudb
 * @Date: 2018-09-27 9:54
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private ArticleContentService articleContentService;
    @Autowired
    private MenuService menuService;

    /**
     *功能描述:跳转到登录页面
     * @author zhoudb
     * @date 2018/8/15 11:24
     * @param
     * @return
     */
    @RequestMapping("/login")
    public String toLogin(){
        return "login";
    }

    /**
     *功能描述: 跳转到未授权页
     * @author zhoudb
     * @date 2018/10/13 21:35
     * @param
     * @return
     */
    @RequestMapping("/unauthorized")
    public String unauthorized() {
        return "error";
    }

    /**
     *功能描述: 跳转到首页
     * @author zhoudb
     * @date 2018/9/6 14:52
     * @param
     * @return
     */
    @RequestMapping("/index")
    public String toAdmin(Model model){
        //查询出所有的一级菜单
        QueryWrapper<Menu> query1 = new QueryWrapper<>();
        query1.eq("level",1);
        query1.orderByAsc("sort_id");
        List<Menu> menuList1=menuService.list(query1);
        //查询出所有的二级菜单
        QueryWrapper<Menu> query2 = new QueryWrapper<>();
        query2.eq("level",2);
        query2.orderByAsc("sort_id");
        List<Menu> menuList2=menuService.list(query2);
        List<Nav> navlist=ConversionUtil.poList2dtoList(menuList1,Nav.class);
        //组装菜单列表
        for(Nav nav:navlist){
            if(CollectionUtils.isEmpty(nav.getMenuList())){
                for(Menu menu2:menuList2){
                    if(nav.getId().equals(menu2.getParentId())){
                        nav.getMenuList().add((Nav) ConversionUtil.po2dto(menu2,Nav.class));
                    }
                }
            }
        }
        //查询出所有的顶部菜单
        QueryWrapper<Menu> query3 = new QueryWrapper<>();
        query3.eq("level",3);
        query3.orderByAsc("sort_id");
        List<Menu> menuList=menuService.list(query3);
        model.addAttribute("navList",navlist);
        model.addAttribute("menuList",menuList);
        return "admin/index";
    }

    /**
     *功能描述: 跳转到欢迎页面
     * @author zhoudb
     * @date 2018/9/6 14:52
     * @param
     * @return
     */
    @RequestMapping("/welcome")
    public String toWelcome(){
        return "admin/welcome";
    }

    /**
     *功能描述: 跳转到菜单管理页面
     * @author zhoudb
     * @date 2018/9/6 14:52
     * @param
     * @return
     */
    @RequestMapping("/menu")
    public String toMenu(){
        return "admin/menu";
    }

    /**
     *功能描述: 跳转到分类管理页面
     * @author zhoudb
     * @date 2018/9/6 14:52
     * @param
     * @return
     */
    @RequestMapping("/category")
    public String toCategory(){
        return "admin/category";
    }

    /**
     *功能描述: 跳转到文章管理页面
     * @author zhoudb
     * @date 2018/9/6 14:52
     * @param
     * @return
     */
    @RequestMapping("/article")
    public String toArticleM(){
        return "admin/article";
    }

    /**
     *功能描述: 跳转到文章详情页
     * @author zhoudb
     * @date 2018/10/13 21:35
     * @param model
     * @param articleId
     * @return
     */
    @RequestMapping("/articleDetail")
    public String toArticleDetail(Model model,String articleId){
        QueryWrapper<ArticleContent> query = new QueryWrapper<>();
        query.eq("article_id",articleId);
        ArticleContent articleContent= articleContentService.getOne(query);
        model.addAttribute("articleId",articleId);
        model.addAttribute("articleContent",articleContent);
        return "admin/articleDetail";
    }

    /**
     *功能描述: 跳转到评论管理页面
     * @author zhoudb
     * @date 2018/9/6 14:52
     * @param
     * @return
     */
    @RequestMapping("/comment")
    public String toComment(){
        return "admin/comment";
    }

    /**
     *功能描述: 跳转到邮件管理页面
     * @author zhoudb
     * @date 2018/9/6 14:52
     * @param
     * @return
     */
    @RequestMapping("/email")
    public String toEmail(){
        return "admin/email";
    }

    /**
     *功能描述: 跳转到邮件新增页面
     * @author zhoudb
     * @date 2018/9/6 14:52
     * @param
     * @return
     */
    @RequestMapping("/emailWrite")
    public String toEmailWrite(){
        return "admin/email-write";
    }

    /**
     *功能描述: 跳转到管理员信息页面
     * @author zhoudb
     * @date 2018/9/6 14:52
     * @param
     * @return
     */
    @RequestMapping("/info")
    public String toAdminInfo(){
        return "admin/info";
    }

    /**
     *功能描述: 跳转到系统管理页面
     * @author zhoudb
     * @date 2018/9/6 14:52
     * @param
     * @return
     */
    @RequestMapping("/system")
    public String toSystem(){
        return "admin/system";
    }



}
